package com.ynding.spring.idea.ddd.core.service.lottery.service.impl;

import com.ynding.spring.idea.ddd.common.aax.bo.ResponseBean;
import com.ynding.spring.idea.ddd.common.service.facade.lottery.result.IssueResult;
import com.ynding.spring.idea.ddd.common.service.integration.lottery.AwardCounterIntegration;
import com.ynding.spring.idea.ddd.common.service.integration.lottery.AwardSendIntegration;
import com.ynding.spring.idea.ddd.common.service.integration.lottery.UserCityInfoIntegration;
import com.ynding.spring.idea.ddd.common.aax.util.LotteryContext;
import com.ynding.spring.idea.ddd.core.model.lottery.domain.DrawLottery;
import com.ynding.spring.idea.ddd.core.model.lottery.valobj.Award;
import com.ynding.spring.idea.ddd.core.model.lottery.valobj.AwardPool;
import com.ynding.spring.idea.ddd.core.model.lottery.valobj.DrawLotteryContext;
import com.ynding.spring.idea.ddd.core.service.lottery.convert.AwardConvert;
import com.ynding.spring.idea.ddd.core.service.lottery.repository.DrawLotteryRepository;
import com.ynding.spring.idea.ddd.core.service.lottery.service.LotteryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author ynding
 * @version 2022/9/11
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class LotteryServiceImpl implements LotteryService {

    private final DrawLotteryRepository drawLotteryRepository;
    private final UserCityInfoIntegration userCityInfoIntegration;
    private final AwardSendIntegration awardSendIntegration;
    private final AwardCounterIntegration awardCounterIntegration;

    /**
     * 这一块是否应该放在 biz-shared模块中？在core-service层中 调用其他领域服务是否合理？ TODO
     * @param lotteryContext 抽奖上下文
     * @return
     */
    @Override
    public IssueResult issueLottery(LotteryContext lotteryContext) {
        //获取抽奖配置聚合根
        DrawLottery drawLottery = drawLotteryRepository.getDrawLotteryById(lotteryContext.getLotteryId());
        //增加抽奖计数信息
        awardCounterIntegration.incrTryCount(lotteryContext);
        //选中奖池
        AwardPool awardPool = drawLottery.chooseAwardPool(buildDrawLotteryContext(drawLottery, lotteryContext));
        //选中奖品
        Award award = awardPool.randomGetAward();
        //发出奖品实体
        return buildIssueResponse(awardSendIntegration.sendAward(AwardConvert.convert2AwardReq(award), lotteryContext));
    }

    /**
     * @param sendAward
     * @return
     */
    private IssueResult buildIssueResponse(ResponseBean sendAward) {
        return new IssueResult();
    }

    /**
     * @param drawLottery    抽奖聚合根
     * @param lotteryContext 抽奖上下文
     * @return
     */
    private DrawLotteryContext buildDrawLotteryContext(DrawLottery drawLottery, LotteryContext lotteryContext) {
        return new DrawLotteryContext();
    }
}
